package com.zdsoft.finance.product.repository;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.zdsoft.finance.product.entity.ProductRepayOrder;
import com.zdsoft.framework.core.common.domain.BaseEntity;
import com.zdsoft.framework.core.common.util.ObjectHelper;

/**
 * 产品还款配置自定义仓库实现
 * @author longwei
 * @date 2016/11/22
 * @version 1.0
 */
public class ProductRepayOrderRepositoryImpl {
	//查询头部
	private StringBuffer hqlBuffer = new StringBuffer("from ProductRepayOrder where 1=1 ");
	
	@PersistenceContext
	private EntityManager em;
	
	/**
	 * 仓库自定义方法实现，通过对象查询
	 * @param productRepayOrder 查询对象
	 * @return 查询结果集合
	 */
	@SuppressWarnings("unchecked")
	public List<ProductRepayOrder> queryByCons(ProductRepayOrder productRepayOrder){
		//条件组装
		StringBuffer whereHqlBuffer = new StringBuffer(" and isDeleted=:isDeleted");
		if(ObjectHelper.isNotEmpty(productRepayOrder.getStatus())){
			whereHqlBuffer.append(" and status = :status ");
		}
		if(ObjectHelper.isNotEmpty(productRepayOrder.getProduct().getId())){
			whereHqlBuffer.append(" and product.id = :productId");
		}
		whereHqlBuffer.append(" order by firstOrder asc");
		
		//参数组装
		Query query = em.createQuery(this.hqlBuffer.toString() + whereHqlBuffer.toString());
		query.setParameter("isDeleted",!BaseEntity.DELETED);
		
		if(ObjectHelper.isNotEmpty(productRepayOrder.getStatus())){
			query.setParameter("status",productRepayOrder.getStatus());
		}
		if(ObjectHelper.isNotEmpty(productRepayOrder.getProduct()) && ObjectHelper.isNotEmpty(productRepayOrder.getProduct().getId())){
			query.setParameter("productId",productRepayOrder.getProduct().getId());
		}
		
		//查询
		List<ProductRepayOrder> lists = query.getResultList();
		return lists;
	}
}
